import LRU from 'lru-cache'
import url from './api.url'
import api from '~api'
import md5 from 'md5'
import qs from 'qs'
const cache = LRU({
  max: 1000,
  maxAge: 1000 * 60 * 15 // 15 min cache
})
export const HomeNewGame = parameter => {
  console.log(` >> 开始请求首页新游速递等数据：`, parameter)
  const key = md5(url.home + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
 
  console.log(url.newGame)
  return api.post(url.newGame, qs.stringify(parameter)).then(response => {
    console.log(response)
    if (parameter.cache) cache.set(key, response)
      return response.data
  })
}
//测试请求
// export const text = parameter => {
//     console.info(` >> 开始请求首页新游速递等数据：`, parameter)
  
//     return api.post(url.credit, qs.stringify(parameter)).then(response => {
//        console.log(response)
//     })
//   }

export const HomeGameNews = parameter => {
  console.info(` >> 开始请求首页游戏资讯等数据：`, parameter)
  const key = md5(url.home + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.home, qs.stringify(parameter)).then(response => {
    if (parameter.cache) cache.set(key, response)
    console.log(JSON.stringify(response))
      return response
  })
}

export const HomeBanner = parameter => {
  console.info(` >> 开始请求首页轮播图等数据：`, parameter)
  const key = md5(url.home + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.carousel, qs.stringify(parameter)).then(response => {
    console.log(JSON.stringify(response))
    if (parameter.cache) cache.set(key, response)
      return response
  })
}
// 精彩活动
export const HomeActivity = parameter => {
  console.info(` >> 开始请求首页精彩活动等数据：`, parameter)
  const key = md5(url.home + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.Activity, qs.stringify(parameter)).then(response => {
    console.log(JSON.stringify(response))
    if (parameter.cache) cache.set(key, response)
      return response
  })
}

export const GameList = parameter => {
  console.info(` >> 开始请求游戏页面游戏列表数据：`, parameter)
  const key = md5(url.game + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.game, qs.stringify(parameter)).then(response => {
    if (parameter.cache) cache.set(key, response)
      return response
  })
}

export const ListData = parameter => {
  console.info(' >> 开始请求榜单页面数据：', parameter)
  const key = md5(url.list + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }

  return api.post(url.list, qs.stringify(parameter)).then(response => {
      console.log(JSON.stringify(response))
    if (parameter.cache) cache.set(key, response)
      return response
  })
}

export const GameDetails = parameter => {
  console.info(' >> 开始请求游戏详情数据：', parameter)
  const key = md5(url.details + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.details, qs.stringify(parameter)).then(response => {
    if (parameter.cache) cache.set(key, response)
      return response
  })
}

export const GiftGame = parameter => {
  console.info(' >> 开始请求游戏礼包数据：', parameter)
  const key = md5(url.gift + JSON.stringify(parameter))
  if (parameter.cache && cache.has(key)) {
    return Promise.resolve(cache.get(key))
  }
  return api.post(url.gift, qs.stringify(parameter)).then(response => {
    if (parameter.cache) cache.set(key, response)
      return response
  })
}
